package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    //新增套餐
    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.addSetmeal(setmeal);

        //获取套餐id
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        setmealDishMapper.addSetmealDish(setmealDishes);
    }

    //套餐分页查询
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //删除套餐
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //判断套餐是否启售，如果启售不能删除
        for (Long id : ids) {
            if (setmealMapper.setmealById(id).getStatus()== StatusConstant.ENABLE)
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }

        //删除套餐表的数据
        setmealMapper.delete(ids);
        //删除套餐菜品关系表的数据
        setmealDishMapper.delete(ids);
    }

    //根据id查询套餐信息和菜品信息
    @Override
    public SetmealVO setmealById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        //根据id查询套餐表信息
        Setmeal setmeal = setmealMapper.setmealById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);

        //根据套餐id查询关联的菜品数据
        List<SetmealDish> setmealDishes = setmealDishMapper.checkSetmealBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    //停售或启售套餐
    @Override
    public void setSetmealStatus(Integer status, Long id) {
        setmealMapper.setSetmealStatus(status,id);
    }

    //修改套餐
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改套餐表信息
        setmealMapper.update(setmeal);

        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));

        //修改套餐表关联的套餐菜品表信息(逻辑为删除原来的数据在增加新的数据)
        ArrayList<Long> setmealIds = new ArrayList<>();
        setmealIds.add(setmealId);
        setmealDishMapper.delete(setmealIds);
        setmealDishMapper.addSetmealDish(setmealDishes);
    }

    //根据分类id查询套餐信息
    @Override
    public List<Setmeal> setmealByCategoryId(Long categoryId) {
        List<Setmeal> setmeals = setmealMapper.setmealByCategoryId(categoryId);
        return setmeals;
    }

    //根据id查询套餐包含的菜品
    @Override
    public List<DishItemVO> dishBySetmealId(Long id) {
        List<DishItemVO> dishItemVOs = setmealDishMapper.dishBySetmealId(id);
        return dishItemVOs;
    }
}
